package com.mobile.web.insuranceApproval;

import com.mobile.service.dto.InsuranceApprovalDTO;
import com.mobile.service.dto.InsuranceApprovalQueryDTO;
import com.mobile.service.inter.InsuranceApprovalService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保险审核表API接口
 */
@RestController
@RequestMapping("/insuranceApproval")
@Tag(name = "保险审核管理接口", description = "包含保险审核的查询、新增、修改、删除等操作")
public class InsuranceApprovalController extends BaseController {

    @Autowired
    private InsuranceApprovalService insuranceApprovalService;

    @PostMapping("/addInsuranceApproval")
    //@RequiresPermissions("insuranceApprovalmana:add:1")
    @Operation(summary = "创建保险审核")
    @AutoFill(OperateType.INSERT)
    public Object addInsuranceApproval(@Valid @RequestBody InsuranceApprovalDTO dto) {
        Map<String, Object> map;
        try {
            map = insuranceApprovalService.addInsuranceApproval(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getInsuranceApprovalListPage")
    //@RequiresPermissions("insuranceApprovalmana:read:1")
    @Operation(summary = "分页查询保险审核")
    public Object getInsuranceApprovalListPage(@Validated InsuranceApprovalQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<InsuranceApprovalDTO> page = insuranceApprovalService.getInsuranceApprovalListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getInsuranceApprovalById")
    //@RequiresPermissions("insuranceApprovalmana:edit:1")
    @Operation(summary = "查询保险审核详情")
    public Object getInsuranceApprovalById(
        @Parameter(description = "保险审核主键", required = true) 
        @RequestParam("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            InsuranceApprovalDTO dto = insuranceApprovalService.getInsuranceApprovalById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateInsuranceApproval")
    //@RequiresPermissions("InsuranceApprovalmana:edit:1")
    @Operation(summary = "编辑保险审核")
    @AutoFill(OperateType.UPDATE)
    public Object updateInsuranceApproval(@Valid @RequestBody InsuranceApprovalDTO dto) {
        Map<String, Object> map;
        try {
            map = insuranceApprovalService.updateInsuranceApproval(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeInsuranceApproval")
    //@RequiresPermissions("insuranceApprovalmana:delete:1")
    public Object removeInsuranceApproval(@RequestParam("id") Integer id) {
        Map<String, Object> map;
        try {
            map = insuranceApprovalService.removeInsuranceApproval(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getInsuranceApprovalList")
    //@RequiresPermissions("insuranceApprovalmana:read:1")
    @Operation(summary = "条件查询保险审核列表")
    public Object getInsuranceApprovalList(@Validated InsuranceApprovalQueryDTO dto) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<InsuranceApprovalDTO> list = insuranceApprovalService.getInsuranceApprovalList(dto);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
